package com.shell.governor.modules.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shell.bee.base.exception.UnCheckException;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.governor.modules.dao.PubMessageReceiverDao;
import com.shell.governor.modules.po.PubMessageReceiver;
import com.shell.governor.modules.po.PubMessageTemplate;
import com.shell.governor.modules.scanner.MessageKeywordScanner;
import com.shell.governor.modules.service.PubMessageTemplateService;
import com.shell.governor.modules.util.MessageTemplateHelper;
import com.shell.governor.modules.vo.KeywordVo;

import tk.mybatis.mapper.entity.Example;

@Service
public class PubMessageTemplateServiceImpl extends PersistentServiceImpl<PubMessageTemplate>
		implements PubMessageTemplateService {
	@Autowired
	PubMessageReceiverDao messageReceiverDao;

	@Pagination
	public List<PubMessageTemplate> selectTemplates(PageLimit limit, PubMessageTemplate messageTemplate) {
		Example example = new Example(PubMessageTemplate.class);
		Example.Criteria criteria = example.createCriteria();
		if ((null != messageTemplate) && (StringUtil.notTrimEmpty(messageTemplate.getMsgTplType()))) {
			criteria.andLike("msgTplType", messageTemplate.getMsgTplType());
		}
		if ((null != messageTemplate) && (StringUtil.notTrimEmpty(messageTemplate.getAppId()))) {
			criteria.andEqualTo("appId", messageTemplate.getAppId());
		}
		return selectByExample(example);
	}

	public PubMessageTemplate selectTemplate(String id) throws UnCheckException {
		PubMessageTemplate temp = (PubMessageTemplate) selectByKey(id);

		Map<String, Class> scanKeywords = MessageKeywordScanner.scanKeywords;
		List<KeywordVo> keywords;
		if (scanKeywords.containsKey(temp.getMsgTplCode())) {
			keywords = MessageTemplateHelper.getKeyword((Class) scanKeywords.get(temp.getMsgTplCode()));
		} else {
			keywords = new ArrayList<KeywordVo>();
		}
		temp.setKeywords(keywords);

		String roleIds = this.messageReceiverDao.selectReceiverIds(id, "0");
		String resIds = this.messageReceiverDao.selectReceiverIds(id, "1");
		temp.setRoleIds(roleIds);
		temp.setResIds(resIds);
		return temp;
	}

	public RetMsg saveTemplate(PubMessageTemplate messageTemplate) {
		updateAll(messageTemplate);
		PubMessageReceiver receiver = new PubMessageReceiver();
		receiver.setMsgTplId(messageTemplate.getMsgTplId());

		this.messageReceiverDao.delete(receiver);
		if (StringUtil.notEmpty(messageTemplate.getResIds())) {
			String[] resIdArr = messageTemplate.getResIds().split(",");
			for (String resId : resIdArr) {
				PubMessageReceiver resRev = new PubMessageReceiver();
				resRev.setMsgTplId(messageTemplate.getMsgTplId());
				resRev.setReceiverId(resId);
				resRev.setReceiverType("1");
				this.messageReceiverDao.insert(resRev);
			}
		}
		if (StringUtil.notEmpty(messageTemplate.getRoleIds())) {
			String[] roleIdArr = messageTemplate.getRoleIds().split(",");
			for (String roleId : roleIdArr) {
				PubMessageReceiver roleRev = new PubMessageReceiver();
				roleRev.setMsgTplId(messageTemplate.getMsgTplId());
				roleRev.setReceiverId(roleId);
				roleRev.setReceiverType("0");
				this.messageReceiverDao.insert(roleRev);
			}
		}
		return new RetMsg("success", "������������������");
	}
}
